<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: ot_shipping.php 135 2013-09-22 07:30:55Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 135 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-22 07:30:55 +0000 (Sun, 22 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

class ot_shipping {
	var $title, $output;
	
	function __construct() {
		global $price, $db;
		$this->code = 'ot_shipping';
		$this->title = MODULE_ORDER_TOTAL_SHIPPING_TITLE;
		$this->description = MODULE_ORDER_TOTAL_SHIPPING_DESCRIPTION;
		$this->enabled = MODULE_ORDER_TOTAL_SHIPPING_STATUS == 'true' ? true : false;
		$this->sort_order = MODULE_ORDER_TOTAL_SHIPPING_SORT_ORDER;
		$this->output = array();
		$this->db = $db;
	}
	
	function process() {
		global $order, $price;
		
		if(defined('MODULE_SHIPPING_FREEAMOUNT_STATUS') && MODULE_SHIPPING_FREEAMOUNT_STATUS == 'True') {
			if((($order->info['total'] - $order->info['shipping_cost']) >= $price->format(MODULE_SHIPPING_FREEAMOUNT_AMOUNT,false,0,true))) {
				$order->info['shipping_method'] = $this->title;
				$order->info['total'] -= $order->info['shipping_cost'];
				$order->info['shipping_cost'] = 0;
			}
		}
	
		$module = substr($_SESSION['shipping']['id'], 0, strpos($_SESSION['shipping']['id'], '_'));
		
		if(!isset($GLOBALS[$module]) && file_exists(DIR_FS_CATALOG.'includes/modules/shipping/'.basename($module).'.php')) {
			include_once(DIR_FS_CATALOG.'includes/modules/shipping/'.basename($module).'.php');
			$GLOBALS[$module] = new $module;
		}
	
		if (not_null($order->info['shipping_method'])) {
			if ($_SESSION['customers_status']['customers_status_show_price_tax'] == 1) {
			
				$shipping_tax = get_tax_rate($GLOBALS[$module]->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
				$shipping_tax_description = get_tax_description($GLOBALS[$module]->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
				$tax = $price->format(add_tax($order->info['shipping_cost'], $shipping_tax), false, 0, false)-$order->info['shipping_cost'];
				
				$order->info['shipping_cost'] = add_tax($order->info['shipping_cost'], $shipping_tax);
				$order->info['tax'] += $tax;
				$order->info['tax_groups'][TAX_ADD_TAX."$shipping_tax_description"] += $tax;
				
				$tax = $price->format($tax,false,0,true);
				
				$order->info['total'] += $tax;
			
			} else {
			
				if ($_SESSION['customers_status']['customers_status_show_price_tax'] == 0 && $_SESSION['customers_status']['customers_status_add_tax_ot'] == 1) {
				
					$shipping_tax = get_tax_rate($GLOBALS[$module]->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
					$shipping_tax_description = get_tax_description($GLOBALS[$module]->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
					$tax = $price->format(add_tax($order->info['shipping_cost'], $shipping_tax), false, 0, false)-$order->info['shipping_cost'];
					
					$order->info['tax'] = $order->info['tax'] += $tax;
					$order->info['tax_groups'][TAX_NO_TAX."$shipping_tax_description"] = $order->info['tax_groups'][TAX_NO_TAX."$shipping_tax_description"] += $tax;
				}
			}
			$this->output[] = array('title' => $order->info['shipping_method'].':',
									'text' => $price->format($order->info['shipping_cost'], true, 0, true),
									'value' => $price->format($order->info['shipping_cost'], false, 0, true));
		}
	}
	
	function check() {
		if (!isset($this->_check)) {
			$check = $this->db->db_query("SELECT configuration_value FROM ".TABLE_CONFIGURATION." WHERE configuration_key = 'MODULE_ORDER_TOTAL_SHIPPING_STATUS'");
			$this->_check = $check->_numOfRows;
		}
		return $this->_check;
	}
	
	function keys() {
		return array('MODULE_ORDER_TOTAL_SHIPPING_STATUS', 
					'MODULE_ORDER_TOTAL_SHIPPING_SORT_ORDER',
					'MODULE_ORDER_TOTAL_SHIPPING_TAX_CLASS');
	}
	
	function install() {
		$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." (configuration_key, configuration_value, configuration_group_id, sort_order, set_function, date_added) VALUES ('MODULE_ORDER_TOTAL_SHIPPING_STATUS', 'true','6', '1','cfg_select_option(array(\'true\', \'false\'), ', NOW())");
		$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." (configuration_key, configuration_value, configuration_group_id, sort_order, date_added) VALUES ('MODULE_ORDER_TOTAL_SHIPPING_SORT_ORDER', '30','6', '2', NOW())");
		$this->db->db_query("INSERT INTO ".TABLE_CONFIGURATION." (configuration_key, configuration_value, configuration_group_id, sort_order, use_function, set_function, date_added) VALUES ('MODULE_ORDER_TOTAL_SHIPPING_TAX_CLASS', '0','6', '3', 'get_tax_class_title', 'cfg_pull_down_tax_classes(', NOW())");      
	}
	
	function remove() {
		$this->db->db_query("DELETE FROM ".TABLE_CONFIGURATION." WHERE configuration_key IN ('".implode("', '", $this->keys())."')");
	}
}
?>